using System;

namespace StackAr.DataStructures
{
	/// <summary>
	/// Test case for StackAr.
	/// </summary>
    /// <remarks>
    /// Taken from the Daikon distribution.
    /// Copyright by Michael Ernst et al.
    /// http://groups.csail.mit.edu/pag/daikon/
    /// 
    /// The original code was written in Java.
    /// We converted it to C# with the Visual Studio conversion assistant.
    /// The intent was to make our C# version as similar to Daikon's
    /// Java version as possible.
    /// 
    /// Please contact us for questions or concerns:
    /// csallner@uta.edu
    /// </remarks>
	public class StackArTester
	{
		private StackAr s = new StackAr(0);
		
		private StackArTester()
		{
		}
		
		public virtual void  doNew(int size)
		{
			s = new StackAr(size);
			observe();
			topAndPop();
		}
		
		private int phase;
		//UPGRADE_NOTE: Final was removed from the declaration of 'maxPhase '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
		private int maxPhase = 5;
		public virtual System.Object createItem(int i)
		{
			switch (phase)
			{
				
				case 0: 
					return new MyInteger(i);
				
				case 1: 
					return (System.Int32) i;
				
				case 2: 
					return (double) i;
				
				case 3: 
					return (float) i;
				
				case 4: 
					return new int[i];
				
				default: 
					return new int[i][];
				
			}
		}
		
		public virtual void  push_noobserve(int i)
		{
			try
			{
				s.push(createItem(i));
			}
			catch (Overflow e)
			{
			}
		}
		
		public virtual void  push(int i)
		{
			try
			{
				s.push(createItem(i));
				observe();
			}
			catch (Overflow e)
			{
			}
		}
		
		public virtual void  topAndPop()
		{
			s.topAndPop();
			observe();
		}
		public virtual void  top()
		{
			s.top();
			observe();
		}
		
		public virtual void  observe()
		{
			bool generatedAux = s.Full;
			bool generatedAux2 = s.Empty;
			s.top();
		}
		
		public virtual void  repPush(int n)
		{
			for (int i = 0; i < n; i++)
			{
				//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
				int x = (int) (1000 * SupportClass.Random.NextDouble());
				push(x);
			}
		}
		
		public virtual void  repPush_noobserve(int n)
		{
			for (int i = 0; i < n; i++)
			{
				//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
				int x = (int) (1000 * SupportClass.Random.NextDouble());
				push_noobserve(x);
			}
		}
		
		public virtual void  repPushOne()
		{
			//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
			int x = (int) (1000 * SupportClass.Random.NextDouble());
			push(x);
		}
		
		public virtual void  popAll()
		{
			while (!s.Empty)
			{
				topAndPop();
			}
			top();
			topAndPop();
		}
		
		public virtual void  makeEmpty()
		{
			observe();
			s.makeEmpty();
			observe();
			top();
			topAndPop();
		}
		
		public virtual void  fillAndEmpty(int n)
		{
			doNew(n);
			repPush(n);
			popAll();
			doNew(n);
			repPush(n);
			makeEmpty();
		}
		
		public virtual void  fillWithSame(int n)
		{
			doNew(n);
			System.Object elt = createItem(n);
			for (int i = 0; i < n; i++)
			{
				try
				{
					s.push(elt);
					observe();
				}
				catch (Overflow e)
				{
				}
			}
			topAndPop();
			// popAll();
		}
		
		public virtual void  run_long()
		{
			for (phase = 0; phase <= maxPhase; phase++)
			{
				fillAndEmpty(0);
				fillAndEmpty(1);
				fillAndEmpty(2);
				fillAndEmpty(5);
				fillAndEmpty(10);
				fillAndEmpty(20);
				fillWithSame(7);
				fillWithSame(22);
			}
			// Avoid invariant about size <= 20
			doNew(22);
			topAndPop();
			repPushOne();
			topAndPop();
			repPush_noobserve(22);
			observe();
			top();
			topAndPop();
			doNew(23);
			// observe();
			repPushOne();
			observe();
			makeEmpty();
		}
		
		public virtual void  run_short()
		{
			phase = 0; fillAndEmpty(0);
			phase = 1; fillAndEmpty(1);
			phase = 2; fillAndEmpty(2);
			phase = 3; fillAndEmpty(5);
			phase = 1; fillAndEmpty(10);
			phase = 2; fillAndEmpty(200);
			phase = 3; fillWithSame(7);
			phase = 4; fillWithSame(22);
		}
		
		public virtual void  run()
		{
			run_long();
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			(new StackArTester()).run();
		}
	}
}